package com.zwz.crawler.riskInfo;

import com.zwz.constant.Constant;
import com.zwz.entity.BusinessAbnormality;
import com.zwz.parse.JsonParserUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.util.List;

public class JsoupRiskRequest {
    public static List<BusinessAbnormality> startJsoupRiskRequest(String enterpriseId ) {
//        String baseUrl = "https://sales.tungee.com/api/enterprise/"+57c94f71d57260d2+"/risk";
        String baseUrl = "https://sales.tungee.com/api/enterprise/"+enterpriseId+"/risk";
        String cookie = "_tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=cc4a07fc-7bb9-40a4-96bc-2cbac34b658f; ..."; // 完整 Cookie

        try {
            // 构建带参数的 URL（GET 请求参数直接拼接）
            String fullUrl = baseUrl + "?status=0";

            // 发送请求（模拟 HTTP/2 伪头为普通头）
            Connection.Response response = Jsoup.connect(fullUrl)
                    .method(Connection.Method.GET)
                    .ignoreContentType(true)  // 允许非 HTML 响应
                    .timeout(30000)
                    // 伪头映射（去掉冒号）
                    .header("authority", "sales.tungee.com")
                    .header("path", "/api/enterprise/57c94f71d57260d2/risk?status=0")
                    // 标准请求头
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("cookie", Constant.cookies)
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/57c94f71d57260d2/enterprise-information/business-risk")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    .execute();

            // 输出结果
            System.out.println("HTTP 状态码: " + response.statusCode());
            System.out.println("响应内容: \n" + response.body());


            //使用实例解析
            List<BusinessAbnormality> list = JsonParserUtil.ParseEception(response.body());

            return list;

        } catch (IOException e) {
            System.err.println("请求失败: " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}